// pages/school/compare_detail/indeXU.js
const {
  schoolContrast,
  enrollInfo,
  planBatch,
  planCategory,
  specialInfo
} = require('../../../api/api')
const {
  decodeUserInfo
} = require('../../../api/login')
const selectorRouteDatas = [{
    id: 0,
    title: "全国",
    type: "radiobox",
    key: "provinceId",
    items: []
  },
  {
    id: 1,
    title: "年份",
    type: "radiobox",
    key: "year",
    items: []
  },
  {
    id: 2,
    title: "历史类",
    key: "type",
    type: 'radiobox',
    items: [],
  },
  {
    id: 3,
    title: "本科批",
    key: "localBatchName",
    type: 'radiobox',
    items: [],
  },


]

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedData: {
      provinceId: 0,
      year: 2021,
      localBatchName: '本科批',
      type: '历史类',
      selectedMajorList:[]
    },
    selectorDataList: selectorRouteDatas,
    checkedList: [],
    columns: [

      {
        key: 'name',
        name: '攻'
      },
      {
        key: 'filter',
        name: '攻'
      },
      {
        key: 'provincesName',
        name: '所在地',
        type: 'empty',
        isVip:true

      },
      {
        key: 'floorSpace',
        name: '占地面积',
        type: 'empty',
        isVip:true

      },
      {
        key: 'schoolYears',
        name: '建校时间',
        type: 'empty',
        isVip:true

      },
      {
        key: 'eduType',
        name: '办学性质',
        type: 'empty',
        isVip:true

      },
      {
        key: 'eduLevelName',
        name: '院校层次',
        type: 'empty',
        isVip:true

      },
      {
        key: 'schoolType',
        name: '院校类型',
        type: 'empty',
        isVip:true

      },
      {
        key: 'isProject985',
        name: '985',
        boolean: true,
        type: 'boolean',
        isVip:true

      },
      {
        key: 'isProject211',
        name: '211',
        boolean: true,
        type: 'boolean',
        isVip:true


      },
      {
        key: 'isDoubleTop',
        name: '双一流',
        boolean: true,
        type: 'boolean',
        isVip:true
      },
      {
        name: '招生信息',
        key: 'spliter',
      },
      {
        key: 'enrollNum',
        name: '招生计划数',
        type: 'empty'
      },
      {
        key: 'addEnrollNum',
        name: '同比扩招',
        type: 'empty'
      },
      // {
      //   key: 'spGroupList',
      //   name: '专业组',
      //   type:'select'
      // },
      // {
      //   key: 'sgInfo',
      //   name: '选课要求'
      // },
      {
        key: 'lowestLocant',
        name: '最低分/位次',
        type: 'empty'
      },
      {
        name: '院校信息',
        key: 'spliter',
      },

      {
        key: 'softCienceRanking',
        name: '软科排名',
        type: 'empty'

      },
      {
        key: 'wuShulianRanking',
        name: '武书连排名',
        type: 'empty'

      },
      {
        key: 'ratio',
        name: '男女比例',
        type: 'empty'

      },
      {
        key: 'graduationDestinationFulfillmentRate',
        name: '就业率',
        type: 'empty'

      },
      {
        key: 'domesticAdmissionRate',
        name: '国内升学率',
        type: 'empty'

      },
      {
        key: 'goingAbroadRate',
        name: '出国率',
        type: 'empty'

      },
      {
        name: '办学信息',
        key: 'spliter',

      },


      {
        key: 'isEducationDept',
        name: '教育部直属',
        boolean: true,
        type: 'boolean'

      },
      {
        key: 'isCentralMinistry',
        name: '中央部委',
        boolean: true,
        type: 'boolean'

      },
      {
        key: 'isStrongPlan',
        name: '强基院校',
        boolean: true,
        type: 'boolean'

      },
     
      {
        key: 'firstLevelMasterDegree',
        name: '一级硕士点',
        type: 'empty'

      },
      {
        key: 'firstLevelDoctoralProgram',
        name: '一级博士点',
        type: 'empty'

      },
      // {
      //   key: 'eduLevel_dictText',
      //   name: '院士数量',
      //   type: 'empty'

      // },
      // {
      //   key: 'eduLevel_dictText',
      //   name: '重点实验室',
      //   type: 'empty'

      // },
      // {
      //   key: 'eduLevel_dictText',
      //   name: '开设专业',
      //   type: 'empty'

      // },
      // {
      //   key: 'eduLevel_dictText',
      //   name: '国家特色专业',
      //   type: 'empty'

      // },
      // {
      //   key: 'eduLevel_dictText',
      //   name: '省级特色专业',
      //   type: 'empty'

      // },
      // {
      //   key: 'eduLevel_dictText',
      //   name: 'A+专业',
      //   type: 'empty'

      // },


    ],
    headWidth: '0',
    schoolIds: [],
    filterRouteKey: 'compareDetailFilters',
    setting: {
      width: '750rpx',
      weight: 'normal',
      color: '#999999',
      fontSize: '26rpx',
      padding: 0
    },
    isVip:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.params) {
      const checkedList = JSON.parse(decodeURIComponent(options.params))
      const schoolIds = []
      checkedList.map(e => {
        schoolIds.push(e.id)
      })
      this.setData({
        checkedList,
        schoolIds
      })
      this.getSchoolContrast()
      const headWidth = checkedList.length * 250 + 200 + 'rpx'
      this.setData({
        headWidth
      })
    }
    const {
      filterRouteKey,
      selectedData,
    } = this.data
    const {
      code
    } = wx.getStorageSync('userCitys')
    selectedData.provinceId = code
    this.setData({
      userCitys: wx.getStorageSync('userCitys'),
      selectedData,
    })
    wx.setStorageSync(filterRouteKey, selectedData)
    this.fetchCity()
    this.getPlanCategory()
  },

  async fetchCity() {
    const {
      selectorDataList,
      selectedData
    } = this.data
    const cityList = wx.getStorageSync('cityList') || await getApp().getCityList()
    selectorDataList[0].items = cityList
    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = c.length > 0 ? c[0].title : item.title
    })
    this.setData({
      selectorDataList,
      selectedData
    })
    // console.log(selectorDataList)
    // this.filterKeyFunc()
  },
  filterKeyFunc() {
    const {
      filterRouteKey,
      selectorDataList,
      planCategorydata,
      planBatchData,
    } = this.data
    const selectedData = wx.getStorageSync(filterRouteKey)
    if (selectedData['provinceId'] !== this.data.selectedData['provinceId']) {
      this.setData({
        selectedData
      })
      wx.setStorageSync(filterRouteKey, selectedData)
      this.getPlanCategory()
      return
    }
    var list = []
    planCategorydata.map((item) => {
      if (item.year == selectedData['year']) {
        list = item.list
        if (selectedData['year'] !== this.data.selectedData['year']) {
          selectedData['type'] = list[0].id
          wx.setStorageSync(filterRouteKey, selectedData)
        }
      }
    })
    var batchList = []
    planBatchData.map((item) => {
      if (item.year == selectedData['year']) {
        batchList = item.list
        if (selectedData['year'] !== this.data.selectedData['year']) {
          selectedData['localBatchName'] = batchList[0].id
          wx.setStorageSync(filterRouteKey, selectedData)
        }
      }
    })
    selectorDataList[2].items = list
    selectorDataList[3].items = batchList
    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = c.length > 0 ? c[0].title : item.title
      // if(item.key!='provinceId'){
      //   planRouteData[item.key] = c.length > 0 ? c[0].title : ''
      // }
    })
    this.setData({
      selectedData,
      selectorDataList
    })
    wx.setStorageSync(filterRouteKey, selectedData)
    this.getEnrollInfo()
  },
  // 获取 年份-类型
  async getPlanCategory() {
    const {
      selectedData,
      filterRouteKey,
      selectorDataList
    } = this.data
    const params = {
      provinceId: selectedData.provinceId,
      schoolId: 0,
    }
    const {
      result
    } = await planCategory(params)
    result.sort((a, b) => {
      return b.year - a.year
    }) //倒序
    if (result.length > 0) {
      result.map((item) => {
        item.list.map((e, i) => {
          let obj = {
            id: e,
            title: e
          }
          item.list[i] = obj
        })
      })
      var yearArr = result.map((item) => {
        return {
          id: item.year,
          title: item.year
        }
      })

      const {
        list
      } = result[0]
      selectedData.year = yearArr[0].id
      selectedData.type = list[0].id
      selectorDataList[1].items = yearArr
      selectorDataList[2].items = list
    }

    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = c.length > 0 ? c[0].title : item.title
    })
    this.setData({
      selectorDataList,
      selectedData
    })
    wx.setStorageSync(filterRouteKey, selectedData)
    this.setData({
      planCategorydata: result,
    })
    this.getPlanBatch()
  },
  // 获取批次
  async getPlanBatch() {
    const {
      selectedData,
      filterRouteKey,
      selectorDataList,
      checkedList
    } = this.data
    const schoolId = checkedList.map(e => {
      return e.id
    }).join('_')
    // console.log(schoolId)
    const params = {
      provinceId: selectedData.provinceId,
      schoolId
    }
    const {
      result
    } = await planBatch(params)
    if (result.length > 0) {
      result.sort((a, b) => {
        return b.year - a.year
      }) //倒序
      result.map((item) => {
        item.list.reverse()
        item.list.map((e, i) => {
          let obj = {
            id: e,
            title: e
          }
          item.list[i] = obj
        })
      })
      const {
        list
      } = result[0]
      selectedData.localBatchName = list[0].id
      selectorDataList[3].items = list
    }
    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = c.length > 0 ? c[0].title : item.title
    })
    this.setData({
      selectorDataList,
      selectedData
    })
    wx.setStorageSync(filterRouteKey, selectedData)
    this.setData({
      planBatchData: result,
    })
    this.getEnrollInfo()
  },

  async onSelectChangedRoute(e) {
    wx.setStorageSync(this.data.filterRouteKey, {
      ...e.detail
    });
    this.setData({
      ...e.detail
    });
    this.filterKeyFunc()

  },
  clearItem(e) {
    const {
      schoolList
    } = this.data
    const {
      index
    } = e.currentTarget.dataset
    schoolList.splice(index, 1)
    const headWidth = schoolList.length * 250 + 200 + 'rpx'
    this.setData({
      schoolList,
      headWidth
    })
  },
  bindPickerChange(e) {
    const {
      schoolList,
      selectedData
    } = this.data
    const {
      i,
      id
    } = e.currentTarget.dataset
    const {
      type,
      localBatchName,
      provinceId,
      year
    } = selectedData
    schoolList[i].index = e.detail.value
    const params = {
      sgNames: [schoolList[i].spGroupList[e.detail.value]],
      schoolIds: [id],
      type,
      localBatchName,
      provinceId,
      year,
    }
    this.setData({
      schoolList
    })
    this.getSpecialInfo(params)
  },
  // 获取选科信息
  async getSpecialInfo(params) {
    const {
      schoolList
    } = this.data
    const {
      result
    } = await specialInfo(params)
    Object.keys(result).map(i => {
      schoolList.map(e => {
        if (i == e.id) {
          e.lowestLocant = ''
          e.sgInfo = ''
          if (result[e.id]) {
            e.sgInfo = result[e.id].sgInfo
            e.lowestLocant = this.getString(result[e.id].min) + '/' + this.getString(result[e.id].minSection)
          }
        }
      })
    })
    this.setData({
      schoolList
    })
  },
  // 获取学校对比院校信息
  async getSchoolContrast() {
    const {
      checkedList
    } = this.data
    const schoolIds = []
    checkedList.map(e => {
      schoolIds.push(e.id)
    })
    const {
      result
    } = await schoolContrast({
      schoolIds
    })
    const schoolList = []
    result.map((e, i) => {
      if (e.school.femaleRate !== '' && e.school.menRate !== '') {
        e.school.ratio = e.school.menRate.split('%')[0] + ' : ' + e.school.femaleRate.split('%')[0]
      }
      e.school.index = 0
      schoolList.push(e.school)
    })
    this.setData({
      schoolList
    })
  },
  getString(str) {
    return str && str != 0 ? str : str = '-'
  },
  // 获取招录信息
  async getEnrollInfo() {
    const {
      schoolList
    } = this.data
    let schoolIds = []
    schoolList.map(e => {
      schoolIds.push(e.id)
    })
    let {
      columns,
      selectedData
    } = this.data
    selectedData.schoolIds = schoolIds
    // console.log(selectedData)
    // return
    const {
      result
    } = await enrollInfo(selectedData)
    let flag = false
    Object.keys(result).map(i => {
      if (result[i].spGroupList) {
        flag = true
      }
      schoolList.map(e => {
        if (i == e.id) {
          e.addEnrollNum = result[e.id].addEnrollNum
          e.lowestLocant = ''
          e.sgInfo = ''
          e.index = 0
          if (result[e.id].provinceScore) {
            e.sgInfo = result[e.id].provinceScore.sgInfo
            e.lowestLocant = this.getString(result[e.id].provinceScore.min) + '/' + this.getString(result[e.id].provinceScore.minSection)
          }
          e.enrollNum = result[e.id].enrollNum
          e.spGroupList = result[e.id].spGroupList
        }
      })
    })
    if (flag) {
      if (columns[13]['key'] !== 'spGroupList') {
        columns.splice(13, 0, {
          key: 'spGroupList',
          name: '专业组',
          type: 'select'
        }, {
          key: 'sgInfo',
          name: '选科要求',
          type: 'empty'
        }, );
      }
    } else {
      if (columns[13]['key'] == 'spGroupList') {
        columns.splice(13, 2)
      }
    }
    this.setData({
      schoolList,
      columns
    })
    // console.log(schoolList)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    this.fetchUserInfo()
  },
  async fetchUserInfo() {
    const {vipType}= await getApp().fetchUserInfo()
    this.setData({
       isVip:vipType,
   }) 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

})